package de.bazinga.rotw.vehicle;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.converters.MilkToJme;

public class RaceCar extends Car {
	private static final long serialVersionUID = 1L;

	public RaceCar(Vector3f startLocation) {
		super(startLocation);
		
	}
	
	
	protected void loadModel() {
		URL jeepUrl = null;
		try {
			jeepUrl = new File("vehicles/race/").toURI().toURL();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		try {
            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_TEXTURE, 
                    new SimpleResourceLocator(jeepUrl));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_MODEL, 
                    new SimpleResourceLocator(jeepUrl));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        
        MilkToJme converter = new MilkToJme();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        URL modelUrl = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "/vehicles/race/f360.ms3d");
        
        Node ant = null;
        try {
            converter.convert(modelUrl.openStream(), out);
            ant = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(out.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ant.setLocalScale(0.1f);
        
        ant.setLocalTranslation(new Vector3f(0.5f,0.1f,-0.1f));
        
        Quaternion rot = new Quaternion();
        rot.fromAngleAxis(FastMath.DEG_TO_RAD*180, Vector3f.UNIT_Y);
        ant.setLocalRotation(rot);
        
        ant.updateRenderState();
		this.attachChild(ant);
	}
}